package br.com.lajeslaboratorio.lajesweb.controller;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.context.RequestContext;

import br.com.lajeslaboratorio.lajesweb.util.FacesUtil;
import br.com.lajeslaboratorio.lajesweb.util.PropertiesUtil;

@ManagedBean
@SessionScoped
public class GenericController implements Serializable {

	/**
	 * Generated Serial Version UID
	 */
	private static final long serialVersionUID = 7153392636641928306L;

	private String msgFinalUC;
	private String navigateTargetUC;
	
	public void showMsgFinalUC() {
		setMsgFinalUC((String) FacesUtil.getHttpSession().getAttribute("msgFinalUC"));
		setNavigateTargetUC((String) FacesUtil.getHttpSession().getAttribute("navigateTargetUC"));
		
		if (getMsgFinalUC() != null && !getMsgFinalUC().equals("") && getNavigateTargetUC() != null && !getNavigateTargetUC().equals("")) {
			
	        RequestContext context = RequestContext.getCurrentInstance();
	        
	        context.update("msgText");
	        context.update("msgButton");
	        context.update("dlgMessage");
	        
	        context.execute("dlgMsg.show();");
	        
	        setFinalUC(null, null);
		}
	}
	
	public void setFinalUC(String msgFinalUC, String navigateTargetUC) {
		FacesUtil.getHttpSession().setAttribute("msgFinalUC", msgFinalUC);
		FacesUtil.getHttpSession().setAttribute("navigateTargetUC", navigateTargetUC);
	}
	
	public void redirectFinalUC(String navigation){
		setMsgFinalUC(null);
		setNavigateTargetUC(null);
		FacesUtil.handleNavigate(navigation);
	}
	
	public void redirect(String navigation){
		FacesUtil.handleNavigate(navigation);
	}
	
	public String getMessageFromKey(String key){
		return PropertiesUtil.getMessageFromKey(key);
	}
	
	public String navigateTo(String navigateTo){
		
		if (navigateTo != null && navigateTo.equals("index")) {
			FacesUtil.getHttpSession().setAttribute("user", null);
		}
		
		return navigateTo;
	}

	/**
	 * @return the msgFinalUC
	 */
	public String getMsgFinalUC() {
		return msgFinalUC;
	}

	/**
	 * @param msgFinalUC the msgFinalUC to set
	 */
	public void setMsgFinalUC(String msgFinalUC) {
		this.msgFinalUC = msgFinalUC;
	}

	/**
	 * @return the navigateTargetUC
	 */
	public String getNavigateTargetUC() {
		return navigateTargetUC;
	}

	/**
	 * @param navigateTargetUC the navigateTargetUC to set
	 */
	public void setNavigateTargetUC(String navigateTargetUC) {
		this.navigateTargetUC = navigateTargetUC;
	}
}
